package cn.iocoder.yudao.module.team.service.linetrip;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.team.controller.admin.linetrip.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.linetrip.LineTripDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.team.dal.mysql.linetrip.LineTripMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.team.enums.ErrorCodeConstants.*;

/**
 * 线路模板—行程 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LineTripServiceImpl implements LineTripService {

    @Resource
    private LineTripMapper lineTripMapper;

    @Override
    public Integer createLineTrip(LineTripSaveReqVO createReqVO) {
        // 插入
        LineTripDO lineTrip = BeanUtils.toBean(createReqVO, LineTripDO.class);
        lineTripMapper.insert(lineTrip);

        // 返回
        return lineTrip.getId();
    }

    @Override
    public void updateLineTrip(LineTripSaveReqVO updateReqVO) {
        // 校验存在
        validateLineTripExists(updateReqVO.getId());
        // 更新
        LineTripDO updateObj = BeanUtils.toBean(updateReqVO, LineTripDO.class);
        lineTripMapper.updateById(updateObj);
    }

    @Override
    public void deleteLineTrip(Integer id) {
        // 校验存在
        validateLineTripExists(id);
        // 删除
        lineTripMapper.deleteById(id);
    }

    @Override
        public void deleteLineTripListByIds(List<Integer> ids) {
        // 删除
        lineTripMapper.deleteByIds(ids);
        }


    private void validateLineTripExists(Integer id) {
        if (lineTripMapper.selectById(id) == null) {
            throw exception(LINE_TRIP_NOT_EXISTS);
        }
    }

    @Override
    public LineTripDO getLineTrip(Integer id) {
        return lineTripMapper.selectById(id);
    }

    @Override
    public PageResult<LineTripDO> getLineTripPage(LineTripPageReqVO pageReqVO) {
        return lineTripMapper.selectPage(pageReqVO);
    }

    @Override
    public void createUpdateBatchLineTrip(List<LineTripDO> updateReqVO) {
        lineTripMapper.insertOrUpdate(updateReqVO);
    }

}